<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/19
 * Time: 20:37
 */

namespace app\admin\model;


class ActivityRoom extends DataBase
{

    public function indexData()
    {
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        );
        $map = true;
        $data = $this->where($map)->paginate($page_config);

        foreach( $data as $key=>$value ){

            $roomInfo = Room::where('id','eq',$value['room_id'])->field('r_id,r_name,user_admin')->find();

            $data[$key]['nickname'] = User::where('id','eq',$roomInfo['r_id'])->value('nickname');

            $data[$key]['r_id'] = $roomInfo['r_id'];

            $data[$key]['r_name'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                return base64_decode($r[1]);
            }, $roomInfo['r_name']);

            $admin = '';
            $user_admin = unserialize($roomInfo['user_admin']);
            if(!empty($user_admin)){

                foreach($user_admin as $kk => $vv){

                    $userInfo = User::where('id','eq',intval($vv))->field('u_id,nickname')->find();

                    $admin.= $userInfo['u_id'].'('.$userInfo['nickname'].')';
                }
            }

            $data[$key]['user_admin'] = $admin;
            $room_judge = unserialize($value['room_judge']);
            $judge = '';
            if(!empty($room_judge)){

                foreach($room_judge as $k => $v){

                    $userInfo = User::where('id','eq',intval($v))->field('u_id,nickname')->find();

                    $judge.= $userInfo['u_id'].'('.$userInfo['nickname'].')';
                }

            }

            $data[$key]['room_judge'] = $judge;

        }
        return $data;

    }

//活动房间删除
    public function activityRoomDel()
    {
        $id = input('id');
        $res = $this->where('id','eq',$id)->delete();
        if($res)  return ['code'=>100,'msg'=>'删除成功'];
        else return ['code'=>-100,'msg'=>'删除失败'];
    }

//活动房间添加
    public function activityRoomAdd()
    {

        $u_id = input('u_id');
        $click_max = input('click_max');
        if( $click_max % 100 != 0 ) return ['code'=>-100,'msg'=>'最大点击数要为100的倍数'];

        $r_id = User::where('u_id','eq',$u_id)->value('id');
        $room_id = Room::where('r_id','eq',$r_id)->value('id');

        if(!$room_id) return ['code'=>-100,'msg'=>'没有找到该房间'];

        $res = $this->save([ 'room_id'=>$room_id,'room_judge'=>serialize([]),'create_time'=>time(),'click_max'=>$click_max ]);
        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];

    }

}